package javaCode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;

/**
 * Created by lxy_920912 on 2017/1/11.
 */
public class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode(int x) { val = x; }
    public static TreeNode  init(int[] vals){
        Queue<TreeNode> queue = new LinkedList<>();
        TreeNode root = new TreeNode(vals[0]);
        queue.add(root);
        for(int i = 1;i < vals.length;i++){
            TreeNode parent = queue.poll();
            if(parent == null){
                i++;
                continue;
            }
            if(vals[i] != -1){
                TreeNode left = new TreeNode(vals[i]);
                parent.left = left;
                queue.add(left);
            }else{
                parent.left = null;
                queue.add(null);

            }
            i++;
            if(i < vals.length){
                if(vals[i] != -1){
                    TreeNode right= new TreeNode(vals[i]);
                    parent.right = right;
                    queue.add(right);
                }else{
                    parent.right = null;
                    queue.add(null);
                }

            }
        }
        return root;
    }
    public void DFS(TreeNode root){
        if(root != null){
            System.out.print(root.val+"->");
            DFS(root.left);
            DFS(root.right);
        }
    }
}
